﻿using ASP;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.IO;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Admin_Admin_GetProduct :  Admin, IRequiresSessionState
{
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.BandUser();
            this.BadnSpecail();
        }
    }
    private void BadnSpecail()
    {
        this.dspecal.DataSource = new SpecialBll().GetAllSpcilNameID();
        this.dspecal.DataTextField = "SpecialName";
        this.dspecal.DataValueField = "SpecialID";
        this.dspecal.DataBind();
    }
    private void BandUser()
    {
        this.duserid.DataSource = new UserBll().GetAllUsers();
        this.duserid.DataTextField = "UserName";
        this.duserid.DataValueField = "UserID";
        this.duserid.DataBind();
    }
    protected void ButNext_Click(object sender, EventArgs e)
    {
        int num = int.Parse(this.duserid.SelectedValue);
        int shopID = this.GetShopID(num.ToString());
        if (num == 0)
        {
            base.Response.Write("<script>alert('请选择用户')</script>");
        }
        else
        {
            if (this.FileOfExecl.PostedFile.FileName == null || this.FileOfExecl.PostedFile.FileName == "")
            {
                base.Response.Write("<script>alert('请选择execl文件')</script>");
            }
            else
            {
                if (string.IsNullOrEmpty(this.ClassID.Value))
                {
                    base.Response.Write("<script>alert('请选择类目')</script>");
                }
                else
                {
                    if (this.dDiverType.SelectedValue == "0")
                    {
                        base.Response.Write("<script>alert('请选择物流方式')</script>");
                    }
                    else
                    {
                        string text = "";
                        int num2 = 0;
                        foreach (ListItem listItem in this.dPayType.Items)
                        {
                            if (listItem.Selected)
                            {
                                if (text == "")
                                {
                                    text += listItem.Value;
                                }
                                else
                                {
                                    text = text + listItem.Value + ",";
                                }
                                num2++;
                            }
                        }
                        if (num2 == 0)
                        {
                            base.Response.Write("<script>alert('请选择付款方式')</script>");
                        }
                        else
                        {
                            if (text.IndexOf(",") > -1)
                            {
                                text = text.Remove(text.LastIndexOf(","));
                            }
                            string fileName = Path.GetFileName(this.FileOfExecl.PostedFile.FileName);
                            string text2 = base.Server.MapPath(".").ToLower().Replace("hl_admin", "") + "UpLoadFile\\" + fileName;
                            this.FileOfExecl.PostedFile.SaveAs(text2);
                            int num3 = text2.LastIndexOf("\\");
                            string path = text2.Substring(0, text2.LastIndexOf("\\"));
                            string extension = Path.GetExtension(text2);
                            if (extension == ".csv" || extension == ".xls")
                            {
                                DataTable dataTable = new DataTable();
                                DataSet dataSet = new DataSet();
                                if (extension == ".csv")
                                {
                                    
                                    ReadExcel readExcel = new ReadExcel(path, fileName);
                                    dataSet = readExcel.Csv();
                                }
                                else
                                {
                                    ReadExcel readExcel = new ReadExcel(text2);
                                    dataSet = readExcel.Execl();
                                }
                                if (dataSet == null || dataSet.Tables.Count == 0)
                                {
                                    base.Response.Write("<script>alert('无法正确读取execel文件')</script>");
                                    return;
                                }
                                dataTable = dataSet.Tables[0];
                                try
                                {
                                    int num4 = 0;
                                    int classID = int.Parse(this.ClassID.Value);
                                    DataTable deliverTypeByUserIDTypeID = new Shop_DeliverType().GetDeliverTypeByUserIDTypeID(num, this.dDiverType.SelectedValue);
                                    decimal num5 = decimal.Parse(deliverTypeByUserIDTypeID.Rows[0]["Charge"].ToString());
                                    int deliverPayType = int.Parse(deliverTypeByUserIDTypeID.Rows[0]["ChargeType"].ToString());
                                    int num6 = int.Parse(this.dDiverType.SelectedValue);
                                    for (int i = 0; i < dataTable.Rows.Count; i++)
                                    {
                                        DataRow dataRow = dataTable.Rows[i];
                                        if (dataRow == null || dataRow["库存量"] == null)
                                        {
                                            break;
                                        }
                                        bool autoPostBack = dataRow["自动重发"].ToString() == "1";
                                        int num7 = int.Parse(dataRow["库存量"].ToString());
                                        decimal auctionStartPrice = decimal.Parse(dataRow["起拍价"].ToString());
                                        DateTime beginDate = DateTime.Parse(dataRow["开始日期"].ToString());
                                        int buyTimes = 0;
                                        string text3 = dataRow["允许购买"].ToString();
                                        string text4 = dataRow["允许浏览"].ToString();
                                        int channelID = int.Parse(this.dspecal.SelectedValue);
                                        string city = dataRow["所在城市"].ToString();
                                        DateTime now = DateTime.Now;
                                        decimal currentPrice = decimal.Parse(dataRow["起拍价"].ToString());
                                        int discount = int.Parse(dataRow["折扣"].ToString());
                                        int discount2 = 1;
                                        int discount3 = 0;
                                        int discount4 = 0;
                                        bool enableSale = dataRow["允许上架"].ToString() == "1";
                                        bool enableSingleSell = dataRow["商城控制是否上架"].ToString() == "1";
                                        DateTime endDate = DateTime.Parse(dataRow["结束日期"].ToString());
                                        bool guarantee = dataRow["是否有保修"].ToString() == "1";
                                        int hits = 0;
                                        int validDay = int.Parse(dataRow["有效期"].ToString());
                                        decimal imcrementPrice = decimal.Parse(dataRow["加价幅度"].ToString());
                                        string inputer = dataRow["输入人"].ToString();
                                        bool isElite = dataRow["是否推荐"].ToString() == "1";
                                        bool isHot = dataRow["是否热点"].ToString() == "1";
                                        string keyword = dataRow["关键字"].ToString();
                                        int limitNum = int.Parse(dataRow["限制购买量"].ToString());
                                        decimal memberPrice = decimal.Parse(dataRow["会员价"].ToString());
                                        bool needInvoice = dataRow["是否开发票"].ToString() == "1";
                                        bool onTop = dataRow["是否热点"].ToString() == "1";
                                        int num8 = int.Parse(dataRow["显示排序"].ToString());
                                        int presentExp = 0;
                                        int presentExp2 = 0;
                                        int presentExp3 = 0;
                                        int presentExp4 = 0;
                                        int value = 0;
                                        int value2 = 0;
                                        int value3 = 0;
                                        int value4 = 0;
                                        int presentPoint = 0;
                                        int presentPoint2 = 0;
                                        int presentPoint3 = 0;
                                        int presentPoint4 = 0;
                                        string producerName = dataRow["生产者名称"].ToString();
                                        string productImageUrl = dataRow["商品图片"].ToString();
                                        string productIntro = dataRow["商品介绍"].ToString();
                                        string productModel = "";
                                        string productName = dataRow["商品名称"].ToString();
                                        string productNum = dataRow["商品编号"].ToString();
                                        string productPlace = dataRow["商品所在地"].ToString();
                                        string productSmallImageUrl = dataRow["商品略缩图"].ToString();
                                        string productStandard = dataRow["商品属性"].ToString();
                                        int num9 = int.Parse(dataRow["商品类型"].ToString());
                                        string propority = "";
                                        string province = dataRow["所在省份"].ToString();
                                        string pUnit = dataRow["单位"].ToString();
                                        string remark = dataRow["备注"].ToString();
                                        int salePromotionType = 0;
                                        int skinID = 0;
                                        decimal standPrice = decimal.Parse(dataRow["一口价"].ToString());
                                        int stars = int.Parse(dataRow["推荐星颗数"].ToString());
                                        int stocks = int.Parse(dataRow["库存量"].ToString());
                                        decimal taxRate = decimal.Parse(dataRow["税率"].ToString());
                                        int num10 = int.Parse(dataRow["团购1当前参加数目"].ToString());
                                        int num11 = int.Parse(dataRow["团购2当前参加数目"].ToString());
                                        int num12 = int.Parse(dataRow["团购3当前参加数目"].ToString());
                                        int num13 = int.Parse(dataRow["团购4当前参加数目"].ToString());
                                        int num14 = int.Parse(dataRow["团购5当前参加数目"].ToString());
                                        string teamName = dataRow["团购名称"].ToString();
                                        int teamNum = int.Parse(dataRow["团购档次"].ToString());
                                        int teamNumber = int.Parse(dataRow["团购1最低参团数目"].ToString());
                                        int teamNumber2 = int.Parse(dataRow["团购2最低参团数目"].ToString());
                                        int teamNumber3 = int.Parse(dataRow["团购3最低参团数目"].ToString());
                                        int teamNumber4 = int.Parse(dataRow["团购4最低参团数目"].ToString());
                                        int teamNumber5 = int.Parse(dataRow["团购5最低参团数目"].ToString());
                                        decimal teamPrice = decimal.Parse(dataRow["团购价1"].ToString());
                                        decimal teamPrice2 = decimal.Parse(dataRow["团购价2"].ToString());
                                        decimal teamPrice3 = decimal.Parse(dataRow["团购价3"].ToString());
                                        decimal teamPrice4 = decimal.Parse(dataRow["团购价4"].ToString());
                                        decimal teamPrice5 = decimal.Parse(dataRow["团购价5"].ToString());
                                        int templateID = 0;
                                        decimal tempSubScriptionRate = decimal.Parse(dataRow["团购预付金比例"].ToString());
                                        string trademarkName = dataRow["商品品牌"].ToString();
                                        DateTime now2 = DateTime.Now;
                                        string uploadFiles = "";
                                        int weight = int.Parse(dataRow["重量"].ToString());
                                        new Shop_Product().AddProduct(shopID, num, channelID, productPlace, productNum, classID, productName, productModel, productStandard, producerName, trademarkName, productIntro, productImageUrl, num9.ToString(), standPrice, auctionStartPrice, imcrementPrice, memberPrice, currentPrice, teamName, teamNum, teamPrice, tempSubScriptionRate, teamNumber, teamPrice2, teamNumber2, teamPrice3, teamNumber3, teamPrice4, teamNumber4, teamPrice5, teamNumber5, isElite, stocks, enableSale, keyword, inputer, salePromotionType, weight, propority, num6.ToString(), num5.ToString(), needInvoice, guarantee, validDay, autoPostBack, remark, deliverPayType, text, text, hits, buyTimes, onTop, isHot, stars, beginDate, endDate, limitNum, enableSingleSell, templateID, skinID, discount, presentExp, presentPoint, value, discount2, presentExp2, presentPoint2, value2, discount3, presentExp3, presentPoint3, value3, discount4, presentExp4, presentPoint4, value4, pUnit, "", productSmallImageUrl, taxRate, uploadFiles, province, city, "", "", "", 0m, "默认", "默认", "", false);
                                        num4++;
                                    }
                                    base.Response.Write("<script>alert('数据出入成功，共导入" + num4.ToString() + "条记录，请核查')</script>");
                                }
                                catch (Exception ex)
                                {
                                    base.Response.Write("<script>alert('数据转换失败，可能电子表格的数据格式不正确，请检查下面的原因：" + ex.Message.ToString() + "')</script>");
                                    return;
                                }
                            }
                            else
                            {
                                base.Response.Write("<script>alert('文件格式不正确')</script>");
                            }
                            File.Delete(text2);
                        }
                    }
                }
            }
        }
    }
    protected void tUserName_TextChanged(object sender, EventArgs e)
    {
        this.duserid.Items.Clear();
        ListItem item = new ListItem("选择用户", "0");
        this.duserid.Items.Add(item);
        this.duserid.DataSource = new UserBll().GetUserLikeUserName(this.tUserName.Text.Trim());
        this.duserid.DataTextField = "UserName";
        this.duserid.DataValueField = "UserID";
        this.duserid.DataBind();
        this.dDiverType.Items.Clear();
        ListItem item2 = new ListItem("选择物流方式", "0");
        this.dDiverType.Items.Add(item2);
        this.dPayType.Items.Clear();
        ListItem listItem = new ListItem("商城支付", "0");
        listItem.Selected = true;
        this.dPayType.Items.Add(listItem);
    }
    private int GetShopID(string UserID)
    {
        int result;
        try
        {
            result = new Shop_Shop().GetShopIDByShopOwerID(int.Parse(UserID));
        }
        catch
        {
            result = 0;
        }
        return result;
    }
    protected void duserid_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.dDiverType.Items.Clear();
        ListItem item = new ListItem("选择物流方式", "0");
        this.dDiverType.Items.Add(item);
        this.dPayType.Items.Clear();
        ListItem listItem = new ListItem("商城支付", "0");
        listItem.Selected = true;
        this.dPayType.Items.Add(listItem);
        int userID = int.Parse(this.duserid.SelectedValue.Trim());
        this.dDiverType.DataSource = new Shop_DeliverType().GetDeliverTypeByUserID(userID);
        this.dDiverType.DataTextField = "TypeName";
        this.dDiverType.DataValueField = "TypeID";
        this.dDiverType.DataBind();
        this.dPayType.DataSource = new Shop_PayType().GetUnDisabledPayTypeByUserID(userID);
        this.dPayType.DataTextField = "TypeName";
        this.dPayType.DataValueField = "TypeID";
        this.dPayType.DataBind();
        if (this.dPayType.Items.Count > 1)
        {
            this.dPayType.Items.RemoveAt(0);
        }
    }
}
